package com.kyxx.mycommonproject.sousuo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kyxx.mycommonproject.R;
import com.kyxx.mycommonproject.base.BaseFragment;
import com.kyxx.mycommonproject.sousuo.adapter.SearchAdapter;
import com.kyxx.mycommonproject.sousuo.presenter.SearchFragmentPresenter;
import com.kyxx.mycommonproject.sousuo.util.SharedPreference;
import com.kyxx.mycommonproject.sousuo.util.Utils;
import com.kyxx.mycommonproject.sousuo.view.ISerachFramentView;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/9/9.
 */
public class SearchFragment extends BaseFragment implements ISerachFramentView{
    @Bind(R.id.img_tool_back)
    ImageView imgToolBack;
    @Bind(R.id.edt_tool_search)
    EditText edtToolSearch;
    @Bind(R.id.img_tool_mic)
    ImageView imgToolMic;
    @Bind(R.id.list_search)
    ListView listSearch;
    @Bind(R.id.txt_empty)
    TextView txtEmpty;
    @Bind(R.id.parent_toolbar_search)
    LinearLayout parentToolbarSearch;
    private Context ctx;
    private View view;
    private ArrayList<String> countryStored;
    private ArrayList<String> mCountries;
    private SearchAdapter searchAdapter;
    private SearchFragmentPresenter presenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.view_toolbar_search, container, false);
        ButterKnife.bind(this, view);
        ctx = getActivity();
        countryStored = SharedPreference.loadList(ctx, Utils.PREFS_NAME, Utils.KEY_COUNTRIES);
        initView();
        setListener();
        logic();
        return view;
    }

    private void initView() {
        edtToolSearch.setHint("请输入关键词");
        presenter = new SearchFragmentPresenter(ctx,this);
    }

    private void logic() {
        countryStored = (countryStored != null && countryStored.size() > 0) ? countryStored : new ArrayList<String>();
        searchAdapter = new SearchAdapter(ctx, countryStored, false);
        listSearch.setVisibility(View.VISIBLE);
        listSearch.setAdapter(searchAdapter);
        Utils.setListViewHeightBasedOnChildren(listSearch);
    }

    private void setListener() {
        listSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                String country = String.valueOf(adapterView.getItemAtPosition(position));
                SharedPreference.addList(ctx, Utils.PREFS_NAME, Utils.KEY_COUNTRIES, country);
                edtToolSearch.setText(country);
                listSearch.setVisibility(View.GONE);
            }
        });

        edtToolSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                listSearch.setVisibility(View.GONE);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length()>0){
                    presenter.getSearchSimilar(s.toString());
                }else{
                    listSearch.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.img_tool_back, R.id.img_tool_mic})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_tool_back:
                edtToolSearch.setText("");
                break;
            case R.id.img_tool_mic:
                presenter.getSearchResult(edtToolSearch.getText().toString());
                break;
        }
    }

    @Override
    public void SearchSimilarData( ArrayList<String> s) {
        listSearch.setVisibility(View.VISIBLE);
        if (s.size()> 0) {
            searchAdapter.updateList(s, true);
        }else {
            listSearch.setVisibility(View.GONE);
            txtEmpty.setVisibility(View.VISIBLE);
            txtEmpty.setText("No data found");
        }
    }

    @Override
    public void SearchResultData() {
        Toast.makeText(ctx,"搜索",Toast.LENGTH_SHORT).show();
    }
}
